//
//  ForbiddenView.m
//  YouinRTCDemo
//
//  Created by 潘欣 on 2023/4/16.
//

#import "ForbiddenView.h"
#import <LSTPopView.h>
#import "ForbiddenCell.h"
@interface ForbiddenView ()<UITableViewDelegate,UITableViewDataSource>
//成员变量用weak修饰, 不可以用strong修饰
@property (nonatomic,weak) LSTPopView *popView;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UILabel *label;
@end

@implementation ForbiddenView

- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/3*2.2-110)];
        view.backgroundColor = [UIColor whiteColor];
        [self addSubview:view];
        
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, SCREEN_WIDTH-30, 30)];
       
        
        self.label.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
        self.label.textColor = [UIColor blackColor];
        self.label.textAlignment = NSTextAlignmentCenter;
        [view addSubview:self.label];
        
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.frame = CGRectMake(0, self.label.frame.origin.y+self.label.frame.size.height+10, SCREEN_WIDTH, view.frame.size.height-30);
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = false;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _tableView.estimatedRowHeight = 100;
        _tableView.rowHeight = 70;//UITableViewAutomaticDimension;
        [_tableView registerClass:[ForbiddenCell class] forCellReuseIdentifier:@"ForbiddenCell"];
        
        [view addSubview:_tableView];
        
        if (@available(iOS 11.0, *)) {
            self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else {
        }
    //    _tableView.transform = CGAffineTransformMakeScale(1, -1);
        
    }
    return self;
}

- (void)setIsBlock:(BOOL)isBlock
{
    _isBlock = isBlock;
    if (self.isBlock) {
        self.label.text = @"黑名单";
    }else{
        self.label.text = @"禁言列表";
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayModel.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ForbiddenCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ForbiddenCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.arrayModel[indexPath.row];
    [cell.removeBtn addTarget:self action:@selector(removeItem:) forControlEvents:UIControlEventTouchUpInside];
    cell.removeBtn.tag = indexPath.row;
    return  cell;
}

- (void)removeItem:(UIButton *)sender
{
    RTCKickOutModel *model = self.arrayModel[sender.tag];
    if(self.isForbidden_words){
        [[YouinRTCManager shardManager] getDelForbiddenWords:@{@"id":model.ID,@"userId":model.userId} successBloc:^(id  _Nonnull json) {
            if([json[@"code"] intValue] == 200){
                [self.arrayModel removeObjectAtIndex:sender.tag];
                [self.tableView reloadData];
                [[ZXMessageCase shareInstace] showMessage:@"移除禁言成功"];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
    }else
    {
        [[YouinRTCManager shardManager] getDelKickOut:model.ID successBloc:^(id  _Nonnull json) {
            if([json[@"code"] intValue] == 200){
                [self.arrayModel removeObjectAtIndex:sender.tag];
                [self.tableView reloadData];
                [[ZXMessageCase shareInstace] showMessage:@"移除黑名单成功"];
            }
            
        } failure:^(NSError * _Nonnull error) {
            
        }];
    }
    
}

-(void)show
{
    LSTPopView *popView = [LSTPopView initWithCustomView:self
                                                popStyle:LSTPopStyleSmoothFromBottom
                                            dismissStyle:LSTDismissStyleSmoothToBottom];
    
    popView.hemStyle = LSTHemStyleBottom;
    popView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    LSTPopViewWK(popView)
    popView.popDuration = 0.5;
    popView.dismissDuration = 0.5;
    popView.isClickFeedback = YES;
    popView.rectCorners = UIRectCornerTopLeft | UIRectCornerTopRight;
    popView.cornerRadius = 15;
    popView.sweepStyle = LSTSweepStyleY_Positive;
    popView.dragStyle = LSTDragStyleY_Positive;
    popView.sweepDismissStyle = LSTSweepDismissStyleSmooth;
    popView.bgClickBlock = ^{
        NSLog(@"点击了背景");
        [wk_popView dismiss];
    };
    self.popView = popView;
    
    [popView pop];
}


- (void)btnAction:(UIButton *)sender
{
    [self.popView dismiss];
}

-(void)dealloc
{
    NSLog(@"dealloc");
}

@end
